﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace devtm.AutoMapper
{

    public static class TypeHelper1
    {


        #region IsOfType

        /// <summary>
        /// Istypes the specified type.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="searchtype">The searchtype.</param>
        /// <returns></returns>
        public static bool IsOfType(this Type type, bool recursif, Type searchtype)
        {
            
            bool r = false;

            if (type == null || searchtype == null)
                return false;

            if (searchtype.IsClass)
                r = type == searchtype || type.IsSubclassOf(searchtype);               
            else if (searchtype.IsInterface)
                r = (type.GetInterface(searchtype.FullName, true) != null);

            if (!r && recursif && type.BaseType != null)
                r = IsOfType(type.BaseType, recursif, searchtype);
            
            return r;
        }


        public static bool IsOfType(this Type type, bool recursif, params Type[] searchtypes)
        {

            foreach (Type searchtype2 in searchtypes)
                if (IsOfType(type, recursif, searchtype2))
                    return true;

            return false;
        }

        #endregion




    }
}
